package com.example.shoppingmall.shoppingcart.fragment;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shoppingmall.R;
import com.example.shoppingmall.base.BaseFragment;
import com.example.shoppingmall.base.GoodsBean;
import com.example.shoppingmall.shoppingcart.adapter.ShoppingCartAdapter;
import com.example.shoppingmall.shoppingcart.utils.CartStorage;

import java.util.List;

// 作用购物车页面的Fragment
public class ShoppingFragment extends BaseFragment implements View.OnClickListener {
    private TextView textView;
    private TextView tvEdit;
    private RecyclerView recyclerview;
    private LinearLayout llCheckAll;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private Button btnCheckOut;
    private LinearLayout llDelete;
    private CheckBox cbAll;
    private Button btnDelete;
    private Button btnCollection;

    private ImageView ivEmpty;
    private TextView tvEmptyCartTobuy;
    // empty_cart.xml
    private LinearLayout ll_empty_shopcart;
    private ShoppingCartAdapter adapter;

    //编辑状态
    private static final int ACTION_EDIT = 1;
    //完成状态
    private static final int ACTION_COMPLETE = 2;

    @Override
    public View initView() {
        Log.e("Tag","购物车的Fragment的UI被初始化");
        View view = View.inflate(mContext, R.layout.fragment_shopping_cart,null);
        tvEdit = (TextView) view.findViewById(R.id.tv_edit);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        llCheckAll = (LinearLayout) view.findViewById(R.id.check_all);
        checkboxAll = (CheckBox) view.findViewById(R.id.checkbox_all);
        tvShopcartTotal = (TextView) view.findViewById(R.id.tv_shopcart_total);
        btnCheckOut = (Button) view.findViewById(R.id.btn_check_out);
        llDelete = (LinearLayout) view.findViewById(R.id.ll_delete);
        cbAll = (CheckBox) view.findViewById(R.id.cb_all);
        btnDelete = (Button) view.findViewById(R.id.btn_delete);
        btnCollection = (Button) view.findViewById(R.id.btn_collection);

        ll_empty_shopcart = (LinearLayout) view.findViewById(R.id.ll_empty_shopcart);
        ivEmpty = (ImageView) view.findViewById(R.id.iv_empty);
        tvEmptyCartTobuy = (TextView) view.findViewById(R.id.empty_cart);

        btnCheckOut.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnCollection.setOnClickListener(this);
        
        initListener();
        return view;
    }

    private void initListener() {
        //设置默认的编辑状态
        tvEdit.setTag(ACTION_EDIT);
        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int action = (int) v.getTag();
                if (action == ACTION_EDIT) {
                    //切换为完成状态
                    showDelete();
                } else {
                    //切换成编辑状态
                    hideDelete();
                }
            }
        });


    }

    private void hideDelete() {
        //1.设置状态和文本-编辑
        tvEdit.setTag(ACTION_EDIT);
        tvEdit.setText("编辑");
        //2.变成非勾选
        if (adapter != null) {
            adapter.checkAll_none(true);
            adapter.checkAll();
            adapter.showTotalPrice();
        }
        //3.删除视图显示
        llDelete.setVisibility(View.GONE);
        //4.结算视图隐藏
        llCheckAll.setVisibility(View.VISIBLE);
    }

    private void showDelete() {
        // 1.设置状态和文本 --完成
        tvEdit.setTag(ACTION_COMPLETE);
        tvEdit.setText("完成");
        // 2.变成非勾选
        if(adapter != null){
            adapter.checkAll_none(false);
            adapter.checkAll();
        }
        // 3.删除视图显示
        llDelete.setVisibility(View.VISIBLE);
        // 4.结算视图隐藏
        llCheckAll.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        super.initData();
        Log.e("Tag","购物车的Fragment的数据被初始化");
        //showData();
    }
    //  生命周期
    @Override
    public void onResume() {
        super.onResume();
        showData();
    }

    /**
     *  显示数据
     * */
    private void showData() {
        List<GoodsBean> goodsBeanList = CartStorage.getInstance().getAllData();
        if(goodsBeanList != null && goodsBeanList.size() >0){
            //tvShopcartEdit.setVisibility(View.VISIBLE);
           // llCheckAll.setVisibility(View.VISIBLE);
            // 有数据
            // 把当没有数据显示的布局-隐藏
            ll_empty_shopcart.setVisibility(View.GONE);
            // 设置设配器
            // 步骤 2
            adapter = new ShoppingCartAdapter(mContext,goodsBeanList,tvShopcartTotal,checkboxAll,cbAll);
            recyclerview.setAdapter(adapter);

            // 设置布局管理器
            recyclerview.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));

        }else{
            // 没有数据
            // 显示数据为空的布局
            ll_empty_shopcart.setVisibility(View.VISIBLE);
            tvEdit.setVisibility(View.GONE);
            llDelete.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        if(view == btnCheckOut){

        }else if(view == btnDelete){
            // 删除选中
            //删除选中的
            adapter.deleteData();
            //校验状态
            adapter.checkAll();
            //数据大小为0
            if(adapter.getItemCount()==0){
                emptyShoppingCart();
            }

        }

    }

    private void emptyShoppingCart() {
        ll_empty_shopcart.setVisibility(View.VISIBLE);
        tvEdit.setVisibility(View.GONE);
        llDelete.setVisibility(View.GONE);
    }
}
